/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
*/
package org.opensearch.test.framework.matcher;

import java.util.Set;
import java.util.stream.Collectors;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;

import org.opensearch.action.search.DeletePitInfo;
import org.opensearch.action.search.DeletePitResponse;

import static java.util.Objects.isNull;

class DeletePitContainsExactlyIdsResponseMatcher extends TypeSafeDiagnosingMatcher<DeletePitResponse> {

    private final Set<String> expectedPitIds;

    DeletePitContainsExactlyIdsResponseMatcher(String[] expectedPitIds) {
        if (isNull(expectedPitIds) || 0 == expectedPitIds.length) {
            throw new IllegalArgumentException("expectedPitIds cannot be null or empty");
        }
        this.expectedPitIds = Set.of(expectedPitIds);
    }

    @Override
    protected boolean matchesSafely(DeletePitResponse response, Description mismatchDescription) {
        Set<String> actualPitIds = response.getDeletePitResults().stream().map(DeletePitInfo::getPitId).collect(Collectors.toSet());
        if (!actualPitIds.equals(expectedPitIds)) {
            mismatchDescription.appendText("Actual pit ids: ").appendValue(actualPitIds);
            return false;
        }
        return true;
    }

    @Override
    public void describeTo(Description description) {
        description.appendText("Should contain exactly pit with ids: ").appendValue(expectedPitIds);
    }
}
